define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'user/user/index',
                    add_url: 'user/user/add',
                    edit_url: 'user/user/edit',
                    // del_url: 'user/user/del',
                    multi_url: 'user/user/multi',
                    table: 'user',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'user.id',
                search: false,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), sortable: true},
                        {field: 'mobile', title: __('Mobile'), operate: 'LIKE'},
                        {field: 'nickname', title: __('Nickname'), operate: 'LIKE'},
                        {field: 'avatar', title: __('Avatar'), events: Table.api.events.image, formatter: Table.api.formatter.image, operate: false},
                        {field: 'birthday', title: __('Birthday'), operate: false},
                        {field: 'green_stone_amount', title: __('Green_stone_amount'), operate: 'between'},
                        {field: 'blue_stone_amount', title: __('Blue_stone_amount'), operate: 'between'},
                        {field: 'red_stone_amount', title: __('Red_stone_amount'), operate: 'between'},
                        {field: 'address', title: __('Address'), operate: false, formatter: Controller.api.formatter.nullorautocontent},
                        {field: 'qrcode', title: __('Qrcode'), operate: false, formatter: Table.api.formatter.image},
                        {field: 'order_num', title: "订单数", operate: false, formatter: function (value, row, index) {
                                url = "activity.order?buy_id=" + row.id;
                                this.url = url;
                                return Table.api.formatter.addtabs.call(this, value, row, index);
                            }},
                        {field: 'is_shop', title: '是否是商家', operate: false, formatter: function (value, row, index) {
                                if (value == 1) {
                                    return '是';
                                } else if (value == 0) {
                                    return '不是';
                                }
                                return '-';
                            }},
                        {field: 'shop_end_time', title: '商家到期日期', operate:'RANGE', addclass:'datetimerange'},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'consume_history_button',
                                    text: '石头消费记录',
                                    classname: 'btn btn-xs btn-info btn-dialog',
                                    url: function (row) {
                                        return Fast.api.fixurl('partner/stone/log/consume?from_id=' + row.id)
                                    },
                                    extend: "data-area='[\"60%\", \"60%\"]'",
                                },
                                {
                                    name: 'delivery_address_button',
                                    text: '收货地址',
                                    classname: 'btn btn-xs btn-info btn-dialog',
                                    url: function (row) {
                                        return Fast.api.fixurl('user/user/deliveryaddress?user_id=' + row.id)
                                    },
                                    extend: "data-area='[\"60%\", \"60%\"]'",
                                },
                                {
                                    name: 'set_tobe_merchant',
                                    text: '设置商家',
                                    classname: 'btn btn-xs btn-info btn-dialog',
                                    url: function (row) {
                                        return Fast.api.fixurl('user/user/setmerchant?user_id=' + row.id)
                                    },
                                    extend: "data-area='[\"60%\", \"60%\"]'",
                                },
                            ],
                            formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        deliveryaddress: function () {
            Controller.api.bindevent();
        },
        setmerchant: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            formatter: {
                showdash: function (value, row, index) {
                    if (value) {
                        return value;
                    }
                    return '-';
                },
                nullorautocontent: function (value, row, index) {
                    if (value) {
                        var width = this.width != undefined ? (this.width.toString().match(/^\d+$/) ? this.width + "px" : this.width) : "250px";
                        var hover = this.hover != undefined && this.hover ? "autocontent-hover" : "";
                        return "<div class='autocontent-item " + hover + "' style='white-space: nowrap; text-overflow:ellipsis; overflow: hidden; max-width:" + width + ";'>" + value + "</div>";
                    }
                    return '-';
                },
            }
        }
    };
    return Controller;
});
